package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


import com.kaishengit.entity.DocumentComment;
import com.kaishengit.entity.User;
import com.kaishengit.service.UserService;
import com.kaishengit.util.db.DBHelp;
import com.kaishengit.util.db.RowMapper;


public class DocumentCommentDao {

	public DBHelp<DocumentComment> db = new DBHelp<DocumentComment>();
	
	public void save(DocumentComment documentComment){
		String sql = "insert into t_document_comment(content,documentId,userId,projectId,createtime) values (?,?,?,?,?)";
		db.executeSQL(sql,documentComment.getContent(),documentComment.getDocumentId(),documentComment.getUserId(),documentComment.getProjectId(),documentComment.getCreatetime());
	}
	
	public void update(DocumentComment documentComment){
		String sql = "update t_document_comment set content = ?, documentId = ?, userId = ?, projectId = ?, createtime = ? where id = ?";
		db.executeSQL(sql, documentComment.getContent(),documentComment.getDocumentId(),documentComment.getUserId(),documentComment.getProjectId(),documentComment.getCreatetime(),documentComment.getId());
	}
	
	public DocumentComment findById(int id){
		String sql = "select content, documentId, userId, projectId,createtime from t_document_comment where id = ?";
		return db.executeQueryToObject(sql, new DocumentCommentRowMapper(), id);
	}
	
	public void del(int id){
		String sql = "delete from t_document_comment where id = ? ";
		db.executeSQL(sql, id);
	}
	
	private class DocumentCommentRowMapper implements RowMapper<DocumentComment>{
		public DocumentComment mapRow(ResultSet rs) throws SQLException {
			DocumentComment documentComment = new DocumentComment();
			documentComment.setId(rs.getInt("id"));
			documentComment.setContent(rs.getString("content"));
			documentComment.setDocumentId(rs.getInt("documentId"));
			documentComment.setUserId(rs.getInt("userId"));
			documentComment.setProjectId(rs.getInt("projectId"));
			documentComment.setCreatetime(rs.getString("createtime"));
			return null;
		}
	}

	public List<DocumentComment> findByDocumentId(int id) {
		String sql = "select * from t_document_comment where documentId=?";
		return db.executeQueryToList(sql, new RowMapper<DocumentComment>() {
			public DocumentComment mapRow(ResultSet resultSet) throws SQLException {
				DocumentComment documentComment = new DocumentComment();
				//documentComment.setId(resultSet.getInt("id"));
				documentComment.setContent(resultSet.getString("content"));
				documentComment.setCreatetime(resultSet.getString("createtime"));
				documentComment.setUserId(resultSet.getInt("userId"));
				documentComment.setDocumentId(resultSet.getInt("documentId"));
				documentComment.setProjectId(resultSet.getInt("projectId"));
				User user = new UserService().findById(resultSet.getInt("userId"));
				documentComment.setUser(user);
				return documentComment;
			}
		},id);
	}

	public void removeByDocumentId(int documentId) {
		String sql="delete from t_document_comment where documentId=?";
		db.executeSQL(sql, documentId);
	}
}
